/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.ui.palette;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.CreationToolEntry;
import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.SelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;

import edu.pku.sei.modeler.model.common.AbstractFactory;
import edu.pku.sei.modeler.model.common.ModelRegistry;
import edu.pku.sei.modeler.model.core.MetaModel;
import edu.pku.sei.modeler.model.structure.MetaDiagram;
import edu.pku.sei.modeler.model.structure.StructureFactory;

/**
 * @author zl
 *
 */

public class DiagramPaletteFactory {
	protected static HashMap<MetaModel, DiagramPaletteFactory>model2factory;
	protected List entries;
	protected AbstractFactory modelFactory;
	protected StructureFactory sFactory;
	
	static
	{
		model2factory = new HashMap<MetaModel, DiagramPaletteFactory>();
	}
	public DiagramPaletteFactory(MetaModel model) {
		try {
			modelFactory = ModelRegistry.getRegistry().getFactory(model);
			sFactory = (StructureFactory)ModelRegistry.getRegistry().getStructureFactory(model);
		} catch(Exception e) {
			e.printStackTrace();
		}
		entries = new ArrayList();
	}
	
	public void addEntry(EntryDescriptor entry) {
		entries.add(entry);
	}
	public PaletteRoot createPalette(MetaDiagram diagram) {
		if(diagram == null) {
			return createDefaultPalette();
		}
		PaletteRoot root = new PaletteRoot();
		PaletteGroup group = new PaletteGroup("Control Group");
		PaletteDrawer drawer = new PaletteDrawer(diagram.getType());
		
		ToolEntry tool = new SelectionToolEntry();
		group.add(tool);
		root.setDefaultEntry(tool);
		tool = new MarqueeToolEntry();
		group.add(tool);
		
		for(int i=0; i<entries.size(); i++) {
			EntryDescriptor entry = (EntryDescriptor)entries.get(i);
			if(entry.diagramType.equals(diagram.getType())) {
				CreationToolEntry toolEntry;
				if(entry.isNode) {
					toolEntry = 
						new CreationToolEntry(
							entry.label,
							entry.shortDesc, 
							new MetaCreationFactory(true, entry.elementType, modelFactory, sFactory), 
							entry.iconSmall, 
							entry.iconLarge);
				}
				else {
					toolEntry = 
						new ConnectionCreationToolEntry(
							entry.label,
							entry.shortDesc,
							new MetaCreationFactory(false, entry.elementType, modelFactory, sFactory),
							entry.iconSmall, 
							entry.iconLarge);
				}
				drawer.add(toolEntry);
			}
		}
		
		root.add(group);
		root.add(drawer);
		return root;
	}

	public static final PaletteRoot createDefaultPalette() {
		PaletteRoot root = new PaletteRoot();
		PaletteGroup group = new PaletteGroup("Control Group");
		PaletteDrawer drawer = new PaletteDrawer("Default Palette");
		
		ToolEntry tool = new SelectionToolEntry();
		group.add(tool);
		root.setDefaultEntry(tool);
		tool = new MarqueeToolEntry();
		group.add(tool);

		root.add(group);
		root.add(drawer);

		return root;
	}
}
